import pymysql
from common.log.log import log
from config.conf import db_config


class MysqlJH:
    def __init__(self):
        # 创建连接
        self.conn = pymysql.connect(**db_config)

    def execute_sql(self, sql):
        try:
            # 创建游标
            cursor = self.conn.cursor()
            cursor.execute(sql)
            # # 获取一条查询结果，以元组的形式返回
            # cursor.fetchone()
            # # 获取三条查询结果
            # cursor.fetchmany(3)
            # # 获取全部查询结果
            # print(cursor.fetchall())
            self.conn.commit()
            return cursor.fetchmany()
        except Exception as e:
            log.error(f'执行sql出现错误，异常为{e}')
            raise e


if __name__ == '__main__':
    sql = 'select * from lk_store_coupon'
    sql_1 = 'select uid from lk_user WHERE account=18665825382'
    result = MysqlJH().execute_sql(sql_1)
    print(result)
